package model;

import entities.Patient;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.JComboBox;
import javax.swing.table.AbstractTableModel;

public class EditPatientTableModel extends AbstractTableModel {

    private ArrayList<Patient> list;
    private ArrayList<Patient> disp;
    private static SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/YYYY");

    public EditPatientTableModel() {
        list = new ArrayList<Patient>();
        disp = new ArrayList<Patient>();
    }

    public EditPatientTableModel(ArrayList<Patient> list) {
        this.list = new ArrayList<Patient>(list);
        this.disp = new ArrayList<Patient>(list);
    }

    @Override
    public int getRowCount() {
        return disp.size();
    }

    @Override
    public int getColumnCount() {
        return 7;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Patient patient = disp.get(rowIndex);
        String male = "male";
        JComboBox cbCell = new JComboBox(new String[]{"View", "Edit", "Delete"});
        if (!patient.isMale()) {
            male = "female";
        }
        Date birthDate = patient.getBirthDate();        
        switch (columnIndex) {
            case 0:
                return patient.getIdentityCard();
            case 1:
                return patient.getName();
            case 2:
                return male;
            case 3:
                if(birthDate != null) {
                    return dateFormat.format(birthDate);                    
                }
                return "";
            case 4:
                return patient.getAddress();
            case 5:
                return patient.getPhone();
            case 6:
                return patient.getModifiedDate();
            default:
                return null;
        }
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return String.class;
    }

    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0:
                return "IdentityCard";
            case 1:
                return "Name";
            case 2:
                return "Gender";
            case 3:
                return "BirthDate";
            case 4:
                return "Address";
            case 5:
                return "Phone";
            case 6:
                return "ModifiedDate";
            default:
                return null;
        }
    }

    @Override
    public void fireTableDataChanged() {
        super.fireTableDataChanged();
    }

    public void addAll(ArrayList<Patient> list) {
        this.list.addAll(list);
        this.disp.addAll(list);
        fireTableDataChanged();
    }

    public void addElement(Patient patient) {
        list.add(patient);
        disp.add(patient);
        fireTableDataChanged();
    }

    public Patient getAt(int index) {
        return disp.get(index);
    }

    public boolean containtID(String id) {
        for (Patient p : list) {
            if (p.getIdentityCard().equals(id)) {
                return true;
            }
        }
        return false;
    }

    public void remove(Patient patient) {
        list.remove(patient);
        disp.remove(patient);
        fireTableDataChanged();
    }

    public void update(int index, Patient patient) {
        list.set(index, patient);
        fireTableDataChanged();
    }

    public boolean update(Patient patient) {
        for (Patient p : list) {
            int id = patient.getPatientID();
            if (p.getPatientID() == id) {
                list.set(list.indexOf(p), patient);
                if (disp.indexOf(p) != -1) {
                    disp.set(disp.indexOf(p), patient);
                }
            }
        }
        fireTableDataChanged();
        return false;
    }

    public void clear() {
        list.clear();
    }

    public void filter(String id, String name) {
        disp.clear();        
        for (Patient p : list) {
            boolean accept = true;
            if (id != null && id.length() != 0) {
                int length = id.length();
                if (p.getIdentityCard().length() >= length) {
                    if (!p.getIdentityCard().substring(0, length).equalsIgnoreCase(id)) {
                        accept = false;
                    }
                } else {
                    accept = false;
                }
            }
            if (name != null && name.length() != 0) {
                int length = name.length();
                if (p.getName().length() >= length) {
                    if (!p.getName().substring(0, length).equalsIgnoreCase(name)) {
                        System.out.println("1");
                        accept = false;
                    }
                } else {
                    accept = false;
                }
            }
            if (accept) {
                disp.add(p);
            }
        }
        fireTableDataChanged();
    }
}
